/**************************************************
 * 希声_music ajax请求后台数据 文件V1.0
 * 编写：翼遥bingo(https://blog.csdn.net/hannah2233)
 * 时间：2022-1-27
 *************************************************/

//api访问地址，可修改
let global_api = "https://netease-cloud-music-api-crete722p-hannah-bingo.vercel.app";

/* 
header区域的函数调用
 */
// ajax加载用户的个人信息
// 参数 ： 账号，密码
function _ajaxUser(user) {
    if (_verify(user)) {
        $.ajax({
                type: "POST",
                url: global_api + "/login/cellphone",
                data: `phone=${user.phone}&password=${user.password}`,
                // 同步请求，报错
                // ache: false,
                // 设置跨域也报错，离谱
                success: function(res) {
                    console.log(res);
                    // 本地存入token
                    // localStorage.setItem('access_token', res['access_token']);
                    if (res.code >= 200 && res.code < 300) {
                        layer.msg("登陆成功！");
                        _ajaxUserInfo(res.profile.userId);
                    } else {
                        layer.msg("密码或者账号有误，请重新输入");
                    }
                    // 请求成功之后自动刷新用户列表

                },
                error: function(reason) {
                    console.log(reason);
                }
            })
            // POST请求方法二
            // $.post(global_api + "/login/cellphone", `phone=${user.phone}&password=${user.password}`).then((res) => {
            //         console.log(res);
            //     }).catch((res) => {
            //         console.log(res);
            //     })
    }
}

// 登陆验证
function _verify(user) {
    //做一个简单的验证
    // console.log(user.password);
    if (user.phone == null || user.phone == "") {
        layer.msg('请输入手机号', { anim: 6 });
        return false;
    } else if (user.password == null || user.password == "") {
        layer.msg('请输入密码', { anim: 6 });
        return false;
    } else if (user.password.length < 6 || user.password.length > 12) {
        layer.msg('密码是6-12位', { anim: 6 });
        return false;
    } else return true;
}

// 获取用户详情
function _ajaxUserInfo(id) {
    $.ajax({
        type: "get",
        url: global_api + "/user/detail",
        data: `uid= ${id}`,
        success: function(res) {
            console.log(res);
            xsN.uid = id; // 记录已同步用户的id
            // console.log(xsN.uid);
            xsN.uname = res.profile.nickname;
            xsN.avatar = res.profile.backgroundUrl;
            // 本地浏览器存储登录用户
            playerSavedata('uid', xsN.uid);
            playerSavedata('uname', xsN.uname);
            playerSavedata('avatar', xsN.avatar);

            userLogin();
        }
    })
}
// 退出登录
function _ajaxLoginOut() {
    $.get(global_api + '/logout').then((res) => {
        console.log(res);
    })
}

/* 
热门推荐 区域的函数调用
 */
// 热门推荐--分类
function _ajaxHotSub() {
    $.get(global_api + "/playlist/hot").then((res) => {
        console.log(res.tags);
        res.tags.unshift({ name: '为您推荐' });
        hotSub = res.tags.splice(0, 6);
        var subHtml = `<span class="h-subt active1">${hotSub[0].name}</span>`;
        for (let i = 1; i < hotSub.length; i++) {
            subHtml += `<span class="h-subt">${hotSub[i].name}</span>`
        }
        $('.h-title').append(subHtml);
    }).catch((reason) => {
        console.log(reason);
    })
}
// 热门推荐 -- 歌单列表
function _ajaxPlayList(params) {
    console.log(params);
    $.ajax({
        type: "get",
        url: global_api + "/top/playlist",
        data: `limit=5&order=hot&cat=${params}&offset=0`,
        success: function(res) {
            console.log(res);
            // xsN.songid = res.playlists[i].id;
            // console.log(xsN.songid);
            var playListHtml = '';
            for (let i = 0; i < 5; i++) {
                playListHtml += `
                <div class="listItem">
                            <div class="itemUp">
                                <img src="${res.playlists[i].coverImgUrl}" alt="" id="image">
                                <span>
                                    <svg t="1643802513691" class="icon" viewBox="0 0 1025 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1180" width="20" height="20"><path d="M510.257 178.266c-139.235 0-252.1 110.566-252.1 246.948 0 136.379 112.865 246.945 252.1 246.945 139.232 0 252.096-110.566 252.096-246.945-0.055-136.382-112.864-246.948-252.096-246.948zM219.136 520.049V292.027c-3.191 0-6.381 0.11-9.462 0.389C261.348 180.222 376.395 102.07 510.257 102.07c133.859 0 248.96 78.152 300.578 190.346-3.135-0.222-6.27-0.39-9.46-0.39V520.05c64.27 0 116.448-51.059 116.448-113.984 0-42.772-24.072-80.001-59.622-99.542C808.149 165.387 671.49 64.055 510.257 64.055c-161.236 0-297.84 101.332-347.948 242.468-35.55 19.485-59.623 56.77-59.623 99.486 0 62.981 52.123 114.04 116.45 114.04z m542.658 130.723c-65.893 60.242-154.292 97.133-251.536 97.133-97.077 0-185.365-36.724-251.203-96.795C141.26 710.454 63.89 816.207 63.89 959.808h895.753c0-143.879-78.771-249.746-197.85-309.036z m0 0" p-id="1181" fill="#ffffff"></path></svg>
                                    <em>${Math.floor(res.playlists[i].playCount/10000)}万</em>
                                </span>
                            </div>
                            <div class="info">
                                <div class="infoName">${res.playlists[i].name}</div>
                                <div class="infoTag"><i>#${res.playlists[i].tags[0]}</i></div>
                            </div>
                            <span class="songId" style="display:none;">${res.playlists[i].id}</span>
                        </div>`

            };
            $("#playlist").html(playListHtml);
            addListbar("nomore");
        },
        error: function(reason) {
            console.error(reason);
        }
    })

}

// 获取歌单详情
// params : 歌曲id
function _ajaxListDetail(params) {
    $.ajax({
        type: 'get',
        url: global_api + '/playlist/detail',
        data: `id=${params}`,
        success: function(res) {
            console.log(res);
            var d_Html = `
            <img class="ItemAva" src="${res.playlist.coverImgUrl}" alt="">
            <div class="describe">
                <h3>${res.playlist.name}</h3>
                <p class="p1"><img src="${res.playlist.creator.avatarUrl}" alt="" class="avatar"><span>${res.playlist.creator.nickname}</span><i>${new Date(res.playlist.createTime).toLocaleString()}</i></p>
                <p class="p2">
                    <svg t="1643819098263" class="icon" viewBox="0 0 1029 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2188" width="15" height="15"><path d="M-2.048 714.752q0-50.176 13.312-87.04t48.128-69.632l0 313.344q-35.84-31.744-48.64-68.608t-12.8-88.064zM957.44 558.08q30.72 23.552 49.664 65.024t18.944 91.648q0 51.2-14.848 87.552t-53.76 69.12l0-313.344zM835.584 512q19.456 0 38.912 15.36t19.456 43.008l0 265.216q0 12.288-5.632 23.04t-13.824 19.456-18.944 13.312-19.968 4.608l-71.68 0q-26.624 0-44.544-16.384t-17.92-44.032l0-265.216q0-27.648 19.968-43.008t42.496-15.36l71.68 0zM249.856 512q29.696 0 48.128 14.336t18.432 45.056l0 258.048q0 30.72-15.872 48.128t-41.472 17.408l-74.752 0q-30.72 0-45.568-18.432t-14.848-45.056l0-260.096q0-30.72 18.432-45.056t44.032-14.336l63.488 0zM509.952 62.464q80.896 0 152.576 27.648t125.44 78.336 84.48 121.344 30.72 155.648l-67.584 0q-25.6 0-46.08 0.512t-20.48-0.512q0-63.488-21.504-112.128t-56.832-81.408-81.92-49.152-98.816-16.384q-51.2 0-97.28 21.504t-81.408 57.856-56.832 82.944-21.504 96.768l-135.168 0q0-84.992 30.72-155.648t83.968-121.344 124.928-78.336 152.576-27.648z" p-id="2189" fill="#dbdbdb"></path></svg>
                    <em>${Math.floor(res.playlist.playCount/10000)}万次</em>
                    <svg t="1643819233739" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3097" width="15" height="15"><path d="M511.913993 928.016126c-28.898371 0-57.968755-12.729044-86.695112-41.455401L106.30472 564.034268C68.117588 516.38636 31.994625 471.318663 31.994625 383.935495c0-141.223585 114.905426-255.956996 255.956996-255.956996 53.15236 0 104.068537 16.169326 147.244079 46.615824 14.449185 10.148833 17.889467 30.102469 7.740635 44.551655-10.148833 14.449185-30.102469 17.889467-44.551655 7.740635-32.338653-22.877877-70.525785-34.918864-110.433059-34.918864-105.960692 0-191.967747 86.179069-191.967747 191.967747 0 61.409037 20.297665 90.135394 59.172854 138.643373l315.645893 318.742147c15.997312 15.997312 28.898371 22.361834 41.283387 22.533848 12.729044 0.172014 24.942046-6.192508 41.455401-22.705863l316.677977-319.946246c25.974131-28.210314 57.968755-75.858223 57.968755-137.439274 0-105.960692-86.179069-191.967747-191.967747-191.967747-47.991937 0-94.091718 17.889467-129.526625 50.22812l-133.998992 134.171006c-12.55703 12.55703-32.682681 12.55703-45.239711 0s-12.55703-32.682681 0-45.239711l135.031077-135.203091c48.335965-44.207626 109.745003-67.945574 173.734252-67.945574 141.223585 0 255.956996 114.905426 255.956996 255.956996 0 82.394759-41.111372 144.319839-75.686209 181.646901L598.781119 886.388712C569.882748 915.287082 540.984378 928.016126 511.913993 928.016126z" p-id="3098" fill="#dbdbdb"></path></svg>
                    <em>${res.playlist.shareCount}</em>
                    <svg t="1643853346854" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4017" width="15" height="15"><path d="M960.167183 449.688839c0-17.253966-13.980409-31.233352-31.234375-31.233352S897.699456 432.434873 897.699456 449.688839c0 177.968297-172.802648 322.749752-385.217479 322.749752-0.548492 0-1.059122 0.13303-1.600451 0.161682-6.865361-0.231267-13.842263 1.647523-19.761066 6.081509L374.512672 866.139669l0-83.289961c0-1.542122-0.237407-3.018753-0.453325-4.499477 2.406816-14.398942-5.573949-28.961612-19.739577-34.328853-137.929396-52.209082-227.055273-167.739329-227.055273-294.332539 0-17.253966-13.980409-31.233352-31.233352-31.233352s-31.233352 13.980409-31.233352 31.233352c0 146.375765 96.360651 279.546787 247.248174 344.541057l0 134.378523c0 11.834536 6.680143 22.642696 17.264199 27.939329 4.422729 2.206248 9.201569 3.294023 13.970176 3.294023 6.628978 0 13.227256-2.115174 18.738783-6.243191l158.490687-118.868271C763.656577 831.018777 960.167183 659.787928 960.167183 449.688839z" p-id="4018" fill="#dbdbdb"></path><path d="M103.473653 374.938368c16.186657 5.876847 34.121122-2.53166 39.987736-18.758226 48.802496-134.971018 200.438055-229.241055 368.766808-229.241055 169.051207 0 320.879148 94.789876 369.224226 230.501768 4.554736 12.780071 16.582677 20.761859 29.424146 20.761859 3.477194 0 7.015787-0.589424 10.482749-1.819438 16.247033-5.78475 24.727171-23.658839 18.941398-39.906895C882.347816 173.778882 710.317765 64.47136 512.228197 64.47136c-197.224874 0-369.021611 108.698654-427.513794 270.480294C78.848813 351.167988 87.246063 369.071753 103.473653 374.938368z" p-id="4019" fill="#dbdbdb"></path><path d="M390.596999 300.54503c-17.253966 0-31.233352 13.980409-31.233352 31.233352l0 30.470989c0 17.253966 13.980409 31.233352 31.233352 31.233352s31.234375-13.980409 31.234375-31.233352l0-30.470989C421.830351 314.524416 407.850965 300.54503 390.596999 300.54503z" p-id="4020" fill="#dbdbdb"></path><path d="M634.366955 300.54503c-17.253966 0-31.234375 13.980409-31.234375 31.233352l0 30.470989c0 17.253966 13.980409 31.233352 31.234375 31.233352s31.233352-13.980409 31.233352-31.233352l0-30.470989C665.60133 314.524416 651.620921 300.54503 634.366955 300.54503z" p-id="4021" fill="#dbdbdb"></path><path d="M589.507258 441.99153c-14.305821-9.405207-33.593096-5.38873-43.159986 8.764618-0.132006 0.193405-13.614066 19.754926-34.172287 19.754926-19.989263 0-32.423457-18.098193-33.267685-19.36914-9.160637-14.417361-28.254507-18.809391-42.834574-9.770528-14.650675 9.109472-19.155269 28.367071-10.055007 43.017746 11.214413 18.047028 41.970904 48.589648 86.157265 48.589648 43.963281 0 75.105558-30.318516 86.574774-48.223305C607.970772 470.358601 603.78238 451.396737 589.507258 441.99153z" p-id="4022" fill="#dbdbdb"></path></svg>
                    <em>${res.playlist.commentCount}</em>
                </p>
                <h5>歌曲简介</h5>
                <p class="p3">${res.playlist.description}
                </p>
            </div>
            `;
            $('.LeftTop').html(d_Html);
        }
    })
}

// 获取歌单评论
// 参数 : 歌单id
function _ajaxComment(params) {
    $.get(global_api + "/comment/playlist", `id=${params}`).then((res) => {
        console.log(res);
        var commentHtml = '';
        for (let i = 0; i < res.comments.length; i++) {
            commentHtml += `
            <div class="commentItem">
                <img src="${res.comments[i].user.avatarUrl}" alt="" class="c_avatar">
                <div class='c_p'>
                    <h5 class="c_name">${res.comments[i].user.nickname}</h5>
                    <p class="c_content">${res.comments[i].content}</p>
                </div>
            </div>
            `
        }
        $('.ItemRight').append(commentHtml);
    })
}

// 获取歌单所有歌曲
function _ajaxListItem(params) {
    $.ajax({
        type: 'get',
        url: global_api + '/playlist/track/all',
        data: `id=${params}`,
        success: function(res) {
            console.log(res);
            var SongItemHtml = '';
            for (let i = 0; i < 50; i++) {
                SongItemHtml += `
                <tr>
                        <td>${i+1}</td>
                        <td>${res.songs[i].name}</td>
                        <td>${res.songs[i].ar[0].name}</td>
                        <td>${res.songs[i].al.name}</td>
                        <td style="display: none;">${res.songs[i].id}</td>
                    </tr>
                `
            }
            $('.SongItem>table').append(SongItemHtml)
        }
    })
}

// 获取歌曲url
function _ajaxSongUrl(params) {
    $.get(global_api + "/song/url", `id=${params}`).then((res) => {
        console.log(res);
        var songUrl = `
        <audio controls preload loop autoplay style="width:90%;">
            <source src="${res.data[0].url}" >
            您的浏览器不支持 audio 元素。
        </audio>
        `;
        $("audio").remove();
        $(".contain").prepend(songUrl);
    })
}

// ajax加载歌词
// 参数 ： 音乐ID
function _ajaxLyric(params, callback) {
    lyricTip('歌词加载中...');
    if (!params) {
        lyricTip('暂无歌词显示，请选择歌曲进行播放')
        return;
    };

    $.get(global_api + "/lyric", `id=${params}`).then((res) => {
        console.log(res);
        if (res.lrc.lyric) {
            callback(res.lrc.lyric, params); // 
            console.log(res.lrc.lyric);
        } else {
            callback('', params);
        }
    })

}